package com.backstage.common.uitil;

import com.backstage.entity.BaseEntity;
import com.backstage.entity.Menu;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * json工具类
 * @Project authority
 * @Package com.backstage.common.uitil
 * @Data 2017/7/26下午 5:37.
 * @Aurhor 阮雪峰
 */
public class JsonUtil {

    private JsonUtil(){}

    /**
     * 实体类转换成json对象
     * @param obj 实体类不包括Collection
     * @param <T>
     */
    public static <T> JSONObject objectToJsonObject(T obj) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        JSONObject json = new JSONObject(mapper.writeValueAsString(obj));
        JSONObject jsonTemp = new JSONObject(mapper.writeValueAsString(obj));
        for (String key : jsonTemp.keySet()) {
            Object value = json.get(key);
            if("null".equals(value.toString())){
                json.remove(key);
            }else{
                if(value instanceof String){
                    String val = value.toString();
                    if("".equals(val)){
                        json.remove(key);
                    }
                }else if(value instanceof JSONArray){
                    JSONArray val = (JSONArray) value;
                    if(val.length() == 0){
                        json.remove(key);
                    }
                }else if(value instanceof JSONObject){
                    objectToJsonObject(value);
                }
            }
        }
        return json;
    }

    /**
     *实体类转换为Map
     * @param obj
     * @param <T>
     * @return
     * @throws JsonProcessingException
     */
    public static <T> Map<String, Object> objectToMap(T obj) throws JsonProcessingException {
        return objectToJsonObject(obj).toMap();
    }

    /**
     * 实体类转换为JsonString
     * @param obj
     * @param <T>
     * @return
     * @throws JsonProcessingException
     */
    public static <T> String objectToJsonString(T obj) throws JsonProcessingException {
        return objectToJsonObject(obj).toString();
    }

    /**
     * List转换为Json数组
     * @param list
     * @param <T>
     * @return
     * @throws JsonProcessingException
     */
    public static <T extends List> JSONArray listToJsonArray(T list) throws JsonProcessingException {
        JSONArray array = new JSONArray();
        for (Object o : list) {
            array.put(objectToJsonObject(o));
        }
        return array;
    }

    /**
     * List转换为Json字符串
     * @param list
     * @param <T>
     * @return
     * @throws JsonProcessingException
     */
    public static <T extends List> String listToJsonString(T list) throws JsonProcessingException {
        return listToJsonArray(list).toString();
    }

    /**
     * String转换成JsonObject
     * @param str
     * @return
     */
    public static JSONObject strJsonToJSONObject(String str){
        return new JSONObject(str);
    }

    /**
     * String转换成Map
     * @param str
     * @return
     */
    public static Map<String, Object> strJsonToMap(String str){
        return new JSONObject(str).toMap();
    }

}
